package com.graduation.smartparkingplatform.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Dao.SpaceMapper;
import com.graduation.smartparkingplatform.Pojo.*;
import com.graduation.smartparkingplatform.Service.SpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpaceServiceImpl implements SpaceService {

  @Autowired SpaceMapper spaceMapper;

  // 获取Space列表
  @Override
  public List<SpaceAll> getSpaceLists() {
    List<SpaceAll> spaceLists = spaceMapper.getSpaceLists();
    return spaceLists;
  }

  // 获取Space列表（分页）
  @Override
  public PageInfo<Space> getSpaceList(Integer page, Integer count) {
    PageHelper.startPage(page, count);

    List<Space> SpaceList = spaceMapper.getSpaceList();

    PageInfo<Space> pageInfo = new PageInfo<>(SpaceList);

    return pageInfo;
  }

  // 获取Space列表（分页）
  @Override
  public PageInfo<SpaceAll> getSpacePublicLists(Integer page, Integer count) {
    PageHelper.startPage(page, count);

    List<SpaceAll> SpaceList = spaceMapper.getSpacePublicLists();

    PageInfo<SpaceAll> pageInfo = new PageInfo<>(SpaceList);

    return pageInfo;
  }

  // 获取该车位下的私有者信息
  public Private getPrivateInfo(Integer spaceId) {
    return spaceMapper.getPrivateInfo(spaceId);
  }

  // 模糊搜索Space（分页）
  @Override
  public PageInfo<Space> searchSpace(Integer page, Integer count, String spaceNo, String carPlate) {
    PageHelper.startPage(page, count);

    List<Space> spaceList = spaceMapper.searchSpace(spaceNo, carPlate);

    PageInfo<Space> pageInfo = new PageInfo<>(spaceList);

    return pageInfo;
  }

  // 添加停车位
  @Override
  public Boolean addSpace(SpaceNA spaceNA) {
    Boolean addSpace = spaceMapper.addSpace(spaceNA);
    Integer spaceID = spaceNA.getSpace_id();
    Integer regionId = spaceNA.getRegion_id();
    Boolean addRegionSpace = spaceMapper.addRegionSpace(spaceID, regionId);
    return addSpace && addRegionSpace;
  }

  // 修改停车位信息
  @Override
  public Boolean changeInfo(SpaceNA spaceNA) {
    Boolean changeInfo = spaceMapper.changeInfo(spaceNA);
    Integer spaceID = spaceNA.getSpace_id();
    Integer regionId = spaceNA.getRegion_id();
    Boolean changeRegionSpace = spaceMapper.changeRegionSpace(spaceID, regionId);
    return changeInfo && changeRegionSpace;
  }

  // 修改停车位锁定状态
  @Override
  public Boolean changeSpaceStatus(Integer spaceId, Boolean status) {
    return spaceMapper.changeSpaceStatus(spaceId, status);
  }

  // 删除停车位
  @Override
  public Boolean deleteSpace(Integer spaceId) {
    Boolean deleteSpace = spaceMapper.deleteSpace(spaceId);
    Boolean deleteRegionSpace = spaceMapper.deleteRegionSpace(spaceId);
    return deleteSpace && deleteRegionSpace;
  }

  // 添加停车车辆
  @Override
  public Boolean addCar(Integer spaceId, String carPlate) {
    return spaceMapper.addCar(spaceId, carPlate);
  }

  // 判断停车位是否为私有
  public Boolean isPrivateSpaceId(Integer spaceId) {
    return spaceMapper.isPrivateSpaceId(spaceId);
  }

  // 获取Space不属于私有车位的列表
  @Override
  public PageInfo<Space> noPrivateSpace(Integer page, Integer count) {
    PageHelper.startPage(page, count);

    List<Space> SpaceList = spaceMapper.noPrivateSpace();

    PageInfo<Space> pageInfo = new PageInfo<>(SpaceList);

    return pageInfo;
  }

  // 模糊搜索Space（分页）
  @Override
  public PageInfo<Space> searchNoPrivateSpace(Integer page, Integer count, String spaceNo) {
    PageHelper.startPage(page, count);

    List<Space> spaceList = spaceMapper.searchNoPrivateSpace(spaceNo);

    PageInfo<Space> pageInfo = new PageInfo<>(spaceList);

    return pageInfo;
  }

  // 获取收费规则
  @Override
  public Charge getCharge(Integer spaceId) {
    return spaceMapper.getCharge(spaceId);
  }

  // 绑定计费规则
  @Override
  public Boolean bindCharge(Integer spaceId, Integer chargeId) {
    try {
      Boolean charge = spaceMapper.deleteCharge(spaceId);
      return spaceMapper.bindCharge(spaceId, chargeId);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
}
